<?php
	include_once("./global.php");
	session_start();

	if(isset($_POST['update_link'])){
		$sql_update = "update linkinfo set `L_Category`='$_POST[link_type]', `L_Title`='$_POST[link_title]',`L_Url`='$_POST[link_url]' where `L_ID` = '$_POST[link_id]'";
		$query_update = $mysqli->query($sql_update);
	}
	
	if(isset($_POST['insert_link'])){
		$sql =  "insert into linkinfo(L_ID,L_Category,L_Title,L_Url) values(NULL,'$_POST[insert_category]','$_POST[insert_title]','$_POST[insert_url]')";
		$query_insert = $mysqli->query($sql);
		if($query_insert){
			echo "<script>alert('添加成功')</script>";
		}else{
			echo "<script>alert('添加失败')</script>";
			echo $mysqli->error;
		}
	}
	
	if($_POST['delete_link']){
		$delete_arr = $_POST['link_id'];
		foreach($delete_arr as $key => $value){
			$value = $delete_arr[$key];
			$sql_delete = "delete from linkinfo where L_ID = '$value'";
			$query_delete = $mysqli->query($sql_delete);
			if($query_delete){
				echo "<script>alert('删除成功')</script>";
			}else{
				echo "<script>alert('删除失败')</script>";
				echo $mysqli->error;
			}
		}
	}
	
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td class="selected"><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
				<tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">友情链接管理</div>
		<form action="" method="post">
		<table style="border-bottom:1px solid #000; width:728px;">
			<tr>
				<td style="width:70px;"><label>友链类型</label></td><td><input type="text" name="insert_category" /></td>
			</tr>
			<tr>
				<td style="width:70px;"><label>友链标题</label><td><input class="input" type="text" name="insert_title" /></td>
			</tr>
			<tr>
				<td style="width:70px;"><label>友链地址</label><td><input class="input" type="text" name="insert_url" /></td>
			</tr>
			<tr>
				<td><input type="submit" name="insert_link" value="提交" /></td>
			</tr>
		</table>
		</form>
		<form name='form1' action="" method="post">
		<table>
			<tr>友链列表</tr>
			<tr>
				<td style="width:100px; border:1px solid #000">友链编号</td>
				<td style="width:100px; border:1px solid #000">友链类型</td>
                <td style="width:100px; border:1px solid #000">友链标题</td>
				<td style="width:220px; border:1px solid #000">友链地址</td>
				<td style="width:100px; border:1px solid #000">操作</td>
			</tr>
            <?php
				$sql = "select * from linkinfo";
				$result = $mysqli->query($sql);
				$total = $result->num_rows;
				$num = 25;
				$page = new Page($total,$num);
				
				$sql_link = "select * from linkinfo {$page->limit_start()}";
				$query_link = $mysqli->query($sql_link);
				while($result_link = $query_link->fetch_array()){
			?>
			<tr>
				<td style="width:120px; border:1px solid #000"><input type="checkbox" name="link_id[]"  value=<?php echo $result_link['L_ID']?> /><?php echo $result_link['L_ID']?></td>
				<td style="width:120px; border:1px solid #000">
					<?php echo $result_link['L_Url']?>
				</td>
				<td style="width:120px; border:1px solid #000">
					<?php echo $result_link['L_Category']?>
				</td>
                <td style="width:120px; border:1px solid #000">
					<?php echo $result_link['L_Title']?>
				</td>

				<td style="width:120px; border:1px solid #000" id="linkupdate"><a href="admin_link_update.php?id=<?php echo $result_link['L_ID']?>">更新</a></td>   
            </tr>
            <?php
            	}
            ?>
            <tr>
            	<td style="width:200px;">
                	<input type="checkbox" id="quan" name="quan" onclick="select_all()" />全选
					<input type="submit" name="delete_link" value="删除" onclick="del()" /> 
				</td>
			</tr>
			<tr>
                	<?php echo $page->pageFirst();?>

                	<?php echo $page->pagePre();?>

                	<?php echo $page->pageList(); ?>

                	<?php echo $page->pageNext();?>

                	<?php echo $page->pageLast();?>

					<?php echo $page->pageTotal();?> 
            </tr>
		</table>
		</form>
	</div>
    </div>
</body>
<script language="javascript">
	function select_all(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(document.getElementById("quan").checked != false){
					checked.checked = true;
				}else{
					checked.checked = false;
				}
			}
		}
	}
	function del(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(checked.checked == true){
					alert(checked.name+checked.value);
				}
			}
		}
	}
	$(document).ready(function(){
		$("table").find("td").click(function(){
			src = $(this).attr("data-option-value");
			if(!$(this).hasClass("selected")){
				$("table").find("td").removeClass("selected");
				$(this).addClass("selected");
				$("iframe").attr("src",src);
			}			
		});
	});
</script>
</html>